/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package webtest;

import java.awt.FlowLayout;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;

class UDPReciever {

    static File fromStream;
    static ImageIcon icon;
    static JLabel lbl;
    static JFrame frame = new JFrame();
    //klient 2

    public static void main(String args[]) throws Exception {
        BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));
        lbl = new JLabel();


        //outgoing transfer
        Thread thread = new Thread(new Runnable() {
            public void run() {
                while (true) {
                    
                    try {
                        DatagramSocket clientSocket;
                        clientSocket = new DatagramSocket();
                        InetAddress IPAddress = InetAddress.getByName("localhost");
                        byte[] sendData = new byte[60000];
                        byte[] receiveData = new byte[60000];
                        String sentence = "Get";//"Get Stream!";//
                        sendData = sentence.getBytes();
                        DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, 9877);
                        clientSocket.send(sendPacket);

                        //incoming transfer
                        DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
                        clientSocket.receive(receivePacket);
                        ByteArrayInputStream input = new ByteArrayInputStream(receiveData);
                        fromStream = new File("bilde.jpg");
                        try (FileOutputStream fos = new FileOutputStream(fromStream)) {
                            int data;
                            while ((data = input.read()) != -1) {
                                char ch = (char) data;
                                fos.write(ch);
                            }
                            fos.flush();
                        }
                        BufferedImage img = ImageIO.read(fromStream);
                        icon = new ImageIcon(img);
                        lbl.setIcon(icon);
                        lbl.repaint();
                        lbl.revalidate();
                        frame.repaint();
                        frame.revalidate();
                    } catch (SocketException ex) {
                        Logger.getLogger(UDPReciever.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (UnknownHostException ex) {
                        Logger.getLogger(UDPReciever.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (IOException ex) {
                        Logger.getLogger(UDPReciever.class.getName()).log(Level.SEVERE, null, ex);
                    }


                }
            }
        });
        thread.start();
        //while (true) {

        //tolke byte arrayen som en fil


        //vis bilde i jframe


        frame.setLayout(new FlowLayout());
        frame.setSize(640, 480);

        lbl.setIcon(icon);
        frame.add(lbl);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        // fromStream.delete();

        //String modifiedSentence = new String(receivePacket.getData());
        //System.out.println("FROM SERVER: " + modifiedSentence);
        System.out.println("Picture recieved!");
        // clientSocket.close();
    }
    //}
}
